package top.lshaci.learning.springboot.jpa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import top.lshaci.learning.springboot.jpa.annotation.JpaResult;
import top.lshaci.learning.springboot.jpa.config.JpaResultConfig;
import top.lshaci.learning.springboot.jpa.entity.Slave;
import top.lshaci.learning.springboot.jpa.vo.SlaveVo;

import java.util.List;
import java.util.Map;

/**
 * SlaveRepository
 *
 * @author liangxu
 */
public interface SlaveRepository extends JpaRepository<Slave, Long> {

    @Query("select s.id as id, s.name as name, s.masterId as masterId, m.name as masterName " +
            "from Slave s " +
            "join Master m on s.masterId = m.id")
    List<Map<String, Object>> find2Map();

    /**
     * 自动生成 SQL 返回自定义对象
     * <pre>
     *     依赖 {@link JpaResult} 和 {@link JpaResultConfig}
     * </pre>
     *
     * @return
     */
    List<SlaveVo> findByName(String name);

    /**
     * 自定义 JPQL 返回自定义对象
     * <pre>
     *     依赖 {@link JpaResult} 和 {@link JpaResultConfig}
     * </pre>
     *
     * @return
     */
    @Query("select s.id as id, s.name as name, s.masterId as masterId, m.name as masterName  " +
            "from Slave s " +
            "join Master m on s.masterId = m.id")
    List<SlaveVo> find2Vo();

    /**
     * 原生 SQL 返回自定义对象
     * <pre>
     *     依赖 {@link JpaResult} 和 {@link JpaResultConfig}
     * </pre>
     *
     * @return
     */
    @Query(value = "select s.id as id, s.name as name, s.master_id as masterId, m.name as masterName  " +
            "from slave s " +
            "join master m on s.master_id = m.id", nativeQuery = true)
    List<SlaveVo> find2VoNative();
}
